package app.maker.controleur;

import java.io.IOException;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import app.maker.entity.Ticket;
import app.maker.entity.User;
import app.maker.error.TicketException;
import app.maker.error.UserException;
import app.maker.service.TicketService;
import app.maker.service.UserService;

/**
 * Servlet implementation class TicketControleur
 */
public class TicketControleur extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private TicketService ticketService;
	private UserService userService;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TicketControleur() {
		super();
		try {
			ticketService = new TicketService();
			userService = new UserService();
		} catch (NamingException e) {
			e.printStackTrace();
		}

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Ticket ticket = new Ticket();
		ticket.setCategorie(request.getParameter("categorie"));
		ticket.setDescription(request.getParameter("description"));
		ticket.setTravauxEffectues(request.getParameter("travaux"));
		int idPilote = Integer.parseInt(request.getParameter("pilote"));
		int idSource = Integer.parseInt(request.getParameter("idSource"));
		try {
			User source = userService.getUser(idSource);
			User pilote = userService.getUser(idPilote);
			ticketService.creer(ticket, source, pilote);
			response.sendRedirect(request.getContextPath()
					+ "/maker/admin/espace-tickets.jsp");
		} catch (UserException e) {
			e.printStackTrace();
		} catch (TicketException e) {
			e.printStackTrace();
		}
	}
}
